<template>
  <div class="activity-calendar">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">活动日历</h1>
        <p class="page-subtitle">展会期间详细的活动安排</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('activity-calendar')"
            :class="['nav-item', { active: activeTab === 'activity-calendar' }]"
          >
            活动日历
          </a>
          <a
            @click="setActiveTab('industry-forum')"
            :class="['nav-item', { active: activeTab === 'industry-forum' }]"
          >
            行业论坛
          </a>
          <a
            @click="setActiveTab('tech-exchange')"
            :class="['nav-item', { active: activeTab === 'tech-exchange' }]"
          >
            技术交流会
          </a>
          <a
            @click="setActiveTab('business-matching')"
            :class="['nav-item', { active: activeTab === 'business-matching' }]"
          >
            商务对接
          </a>
          <a
            @click="setActiveTab('product-release')"
            :class="['nav-item', { active: activeTab === 'product-release' }]"
          >
            产品发布
          </a>
          <a
            @click="setActiveTab('activity-overview')"
            :class="['nav-item', { active: activeTab === 'activity-overview' }]"
          >
            其他活动
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="calendar-section">
        <!-- <h2 class="section-title">活动日历</h2> -->
        <div class="calendar-content">
          <div class="activity-list">
            <div class="activity-row" v-for="item in activities" :key="item.id">
              <div>
                <div class="activity-poster">
                  <img
                    :src="item.posterUrl || defaultPoster"
                    :alt="item.title"
                  />
                </div>
                <div style="margin-top: 20px">
                  <el-button type="primary" @click="openDialog(item)"
                    >立即报名</el-button
                  >
                </div>
              </div>
              <div class="activity-main">
                <div class="activity-title">{{ item.title }}</div>
                <div class="activity-time">{{ item.timeRange }}</div>
                <div class="activity-meta">
                  <span class="meta-item"
                    >地点：{{ item.location || "-" }}</span
                  >
                  <span class="meta-item"
                    >容量：{{ item.capacity ?? "-" }}</span
                  >
                  <span class="meta-item"
                    >报名人数：{{ item.registrationCount ?? "-" }}</span
                  >
                </div>
              </div>
            </div>
            <div v-if="!activities.length" class="empty-tips">暂无活动</div>
          </div>
        </div>
      </section>
    </div>
    <!-- <AutoDialog
      title="活动报名"
      ref="dialogRef"
      @close="close"
      @submit="submit"
    >
      <template #content>
        <AutoForm ref="formRef" />
      </template>
    </AutoDialog> -->
    <ActivityRegistrationDialog
      v-model="registrationDialogVisible"
      :activity-id="currentActivity?.id || null"
      :activity-title="currentActivity?.title || ''"
      activityPlatType="活动日历"
      @success="handleRegistrationSuccess"
    />
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { activityApi, subpageApi } from "@/api/user";
import AutoDialog from "../../components/AutoDialog.vue";
import AutoForm from "../../components/AutoForm.vue";
import ActivityRegistrationDialog from "@/components/ActivityRegistrationDialog.vue";

export default {
  name: "ActivityCalendar",
  components: {
    AutoDialog,
    AutoForm,
    ActivityRegistrationDialog,
  },
  setup() {
    const registrationDialogVisible = ref(false);
    const currentActivity = ref(null);

    const dialogRef = ref();
    const formRef = ref();
    const activeTab = ref("activity-calendar");
    const activities = ref([]);
    const defaultPoster = ref(
      "https://via.placeholder.com/320x200/E9ECFF/320063?text=Activity"
    );
    const bannerImage = ref("");

    const loadActivities = async () => {
      try {
        const res = await activityApi.getActivityList({ status: "published" });
        console.log(res.data, "dasdad");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data)
        ) {
          activities.value = res.data.map((a, idx) => ({
            id: a.id || idx + 1,
            title: a.title || a.name || "",
            startTime: a.startTime || "",
            endTime: a.endTime || "",
            timeRange:
              a.startTime && a.endTime
                ? `${a.startTime} ~ ${a.endTime}`
                : a.startTime || "",
            location: a.location || "",
            capacity: a.capacity ?? a.maxParticipants ?? null,
            registrationCount: a.registrationCount ?? a.registeredCount ?? null,
            posterUrl: a.posterUrl || a.imageUrl || "",
          }));
        } else {
          activities.value = [];
        }
      } catch (e) {
        activities.value = [];
      }
    };

    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_activities");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    const openDialog = (item) => {
      console.log(item);

      // dialogRef.value.open();
      currentActivity.value = item;
      registrationDialogVisible.value = true;
    };

    const submit = () => {
      formRef.value
        .validate()
        .then((res) => {
          dialogRef.value.close();
          console.log("se");
        })
        .catch((err) => {
          console.log(err);
        });
    };

    // 报名成功回调
    const handleRegistrationSuccess = () => {
      console.log("报名成功:", currentActivity.value?.title);
    };

    const close = () => {
      formRef.value.resetFields();
      dialogRef.value.close();
    };

    onMounted(() => {
      loadActivities();
      loadBanner();
    });

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "activity-calendar") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      dialogRef,
      formRef,
      activeTab,
      bannerImage,
      activities,
      defaultPoster,
      setActiveTab,
      openDialog,
      submit,
      close,
      currentActivity,
      registrationDialogVisible,
      handleRegistrationSuccess,
    };
  },
};
</script>

<style scoped>
.activity-calendar {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.calendar-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.calendar-content {
  max-width: 1000px;
  margin: 0 auto;
}

.activity-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.activity-row {
  display: grid;
  grid-template-columns: 240px 1fr;
  align-items: start;
  gap: 16px;
  padding: 14px;
  background: #ffffff;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: 1px solid #eef2ff;
}

.activity-row:hover {
  transform: translateY(-5px);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.12);
}

.activity-poster {
  width: 100%;
  position: relative;
  padding-bottom: 60%;
  max-height: 144px;
}
.activity-poster img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 12px;
}

.activity-main {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.activity-title {
  font-size: 1.15rem;
  color: var(--e-global-color-title);
  font-weight: 700;
}

.activity-meta {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  color: #555;
  font-size: 0.95rem;
}

.meta-item {
  position: relative;
}

@media (max-width: 768px) {
  .calendar-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .calendar-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .activity-list {
    grid-template-columns: 1fr;
  }
  .activity-row {
    grid-template-columns: 1fr;
  }
  .activity-poster {
    height: 180px;
  }

  .page-title {
    font-size: 2rem;
  }

  .page-subtitle {
    font-size: 1rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
